﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace GroupByTest
{
    /// <summary>
    /// To test LINQ group-by for sorting AFX (Active File Explorer) tree view items
    /// </summary>
    /// <remarks>
    /// I have never used LINQ group-by but it looks like I can use this to group active file document collection in SSMS...
    /// </remarks>
    public class Program
    {
        public static void Main(string[] args)
        {
            //var personGroups = from p in GetPersons()
            //              group p by p.Age into g
            //              select new {Age = g.Key, Persons = g};
            //foreach (var personGroup in personGroups)
            //{
            //    Console.WriteLine("Age = {0}", personGroup.Age);
            //    foreach (Person person in personGroup.Persons)
            //    {
            //        PrintPerson(person);
            //    }
            //}

            var names = from IName n in GetNames()
                        group n by n
        }

        private static void PrintPerson(Person person)
        {
            Console.WriteLine("\tName = {0}, Age = {1}", person.Name, person.Age);
        }

        private static IEnumerable<Person> GetPersons()
        {
            var rand = new Random();
            for (int i = 0; i < 50; i++)
            {
                yield return new Person
                                 {
                                     Name = string.Format("Person #{0}", i + 1),
                                     Age = rand.Next(20, 30),
                                 };
            }
        }
    }

    public interface IName
    {
        string Name { get; set; }
    }

    public class Animal : IName
    {
        public string Name { get; set; }
        public string Kind { get; set; }
    }

    public class Person : IName
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
}
